#include "MoCmString8.h"
#include "MoCmLanguage.h"

MO_NAMESPACE_BEGIN

//==================================================
MString8sC::MString8sC(){
   _count = 0;
   _pStrings = MO_CREATE(FString8Vector);
}

//==================================================
MString8sC::~MString8sC(){
   TInt stringCount = _pStrings->Count();
   FString8* pString = NULL;
   for(TInt n = 0; n < stringCount; n++){
      pString = _pStrings->Get(n);
      MO_DELETE(pString);
   }
   MO_DELETE(_pStrings);
}

//==================================================
TChar8C* MString8sC::operator[](TInt index) const{
   MO_ASSERT_RANGE(index, 0, _count);
   FString8* pString = _pStrings->Get(index);
   return pString->MemoryC();
}

//==================================================
TBool MString8sC::IsEmpty() const{
   return 0 == _count;
}

//==================================================
TInt MString8sC::Count() const{
   return _count;
}

//==================================================
TString8sIteratorC MString8sC::IteratorC() const{
   //return TString8sIteratorC(_pStrings->MemoryC(), _count);
   return TString8sIteratorC();
}

//==================================================
TChar8C* MString8sC::First() const{
   if( 0 == _count){
      return NULL;
   }
   FString8* pString = _pStrings->Get(0);
   return pString->MemoryC();
}

//==================================================
TChar8C* MString8sC::Last() const{
   if(0 == _count){
      return NULL;
   }
   FString8* pString = _pStrings->Get(_count - 1);
   return pString->MemoryC();
}

//==================================================
TChar8C* MString8sC::Get(TInt index) const{
   MO_ASSERT_RANGE(index, 0, _count);
   FString8* pString = _pStrings->Get(index);
   return pString->MemoryC();
}

//==================================================
TInt MString8sC::IndexOf(TChar8C* pValue) const{
   for(TInt n = 0; n < _count; n++){
      if(_pStrings->Get(n)->Equals(pValue)){
         return n;
      }
   }
   return ENotFound;
}

//==================================================
TString8 MString8sC::Join(TChar8 splitter){
   TString8 result;
   for(TInt n = 0; n < _count; n++){
      if(n){
         result.Append(splitter);
      }
      result.Append(_pStrings->Get(n)->MemoryC());
   }
   return result;
}

//==================================================
TString8 MString8sC::Join(TChar8C* splitter){
   TString8 result;
   for(TInt n = 0; n < _count; n++){
      if(n){
         result.Append(splitter);
      }
      result.Append(_pStrings->Get(n)->MemoryC());
   }
   return result;
}

//==================================================
TInt MString8sC::Pack(TChar8* pPack, TInt length){
   TInt valueLen, valueLenLen, byteCount = 0;
   FString8* pString;
   for(TInt n = 0; n < _count; n++){
      pString = _pStrings->Get(n);
      valueLen = pString->Length();
      valueLenLen = RInt::CountDigit(valueLen);
      if(pPack){
         TChar8 buffer[16];
         RRuntime::IntToString8(valueLenLen, buffer, 16);
         pPack[byteCount++] = buffer[0];
         RRuntime::IntToString8(valueLen, buffer, 16);
         MO_LIB_MEMCPY(&pPack[byteCount], length - byteCount, buffer, valueLenLen);
         byteCount += valueLenLen;
         MO_LIB_MEMCPY(&pPack[byteCount], length - byteCount, pString->MemoryC(), valueLen);
         byteCount += valueLen;
      }else{
         byteCount += (1 + valueLenLen + valueLen);
      }
   }
   if(pPack){
      pPack[byteCount] = 0;
   }
   return byteCount;
}

//==================================================
TString8 MString8sC::Pack(){
   TString8 result;
   TInt valueLen, valueLenLen;
   FString8* pString;
   for(TInt n = 0; n < _count; n++){
      pString = _pStrings->Get(n);
      valueLen = pString->Length();
      valueLenLen = RInt::CountDigit(valueLen);
      TChar8 buffer[16];
      result.Append(RRuntime::IntToString8(valueLenLen, buffer, 16));
      result.Append(RRuntime::IntToString8(valueLen, buffer, 16));
      result.Append(pString->MemoryC());
   }
   return result;
}

MO_NAMESPACE_END
